package com.xrit.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xrit.mapper.MemberMapper;
import com.xrit.mapper.OrderMapper;
import com.xrit.mapper.SetMealMapper;
import com.xrit.pojo.MemberExample;
import com.xrit.pojo.OrderExample;
import com.xrit.service.ReportService;
import com.xrit.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetMealMapper setMealMapper;


    //使用模拟数据测试流程是否可以正常展示
    /**
     * reportData: {
     *  reportDate: null,//报表日期
     *  todayNewMember: 0,//今日新增会员数
     *  totalMember: 0,//总会员数
     *  thisWeekNewMember: 0,//本周新增会员
     *  thisMonthNewMember: 0,//本月新增会员
     *  todayOrderNumber: 0,//今日预约数
     *  todayVisitsNumber: 0,//今日到诊数
     *  thisWeekOrderNumber: 0,//本周预约数
     *  thisWeekVisitsNumber: 0,//本周到诊数
     *  thisMonthOrderNumber: 0,//本月预约数
     *  thisMonthVisitsNumber: 0,//本月到诊数
     *  hotSetmeal: []
     * }
     */
    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        Map<String,Object> map=new HashMap<>();
        //获取今日会员数
        MemberExample me=new MemberExample();
        MemberExample.Criteria mec = me.createCriteria();
        mec.andRegtimeEqualTo(DateUtils.getToday());
        //今日新增===============================================================
        long todayNewMember = memberMapper.countByExample(me);
        //总会员=================================================================
        long totalMember = memberMapper.countByExample(null);//总会员

        //获取本周一==============================================================
        Date thisWeekMonday = DateUtils.getThisWeekMonday(new Date());
        MemberExample me2=new MemberExample();
        MemberExample.Criteria mec2 = me2.createCriteria();
        mec2.andRegtimeGreaterThanOrEqualTo(thisWeekMonday);
        //本周新增会员============================================================
        long thisWeekNewMember = memberMapper.countByExample(me2);
        //获取本月一号============================================================
        Date firstDay4ThisMonth = DateUtils.getFirstDay4ThisMonth();
        MemberExample me3=new MemberExample();
        MemberExample.Criteria mec3 = me3.createCriteria();
        mec3.andRegtimeGreaterThanOrEqualTo(firstDay4ThisMonth);
        //本周新增会员============================================================
        long thisMonthNewMember = memberMapper.countByExample(me3);

        OrderExample oe=new OrderExample();
        OrderExample.Criteria oec = oe.createCriteria();
        //本日预约================================================================
        oec.andOrderdateEqualTo(DateUtils.getToday());
        long todayOrderNumber = orderMapper.countByExample(oe);
        oec.andOrderstatusEqualTo("已到诊");
        //本日到诊================================================================
        long todayVisitsNumber = orderMapper.countByExample(oe);

        OrderExample oe2=new OrderExample();
        OrderExample.Criteria oec2 = oe2.createCriteria();
        //本周预约================================================================
        oec2.andOrderdateGreaterThanOrEqualTo(thisWeekMonday);
        long thisWeekOrderNumber = orderMapper.countByExample(oe2);
        oec2.andOrderstatusEqualTo("已到诊");
        //本周到诊================================================================
        long thisWeekVisitsNumber = orderMapper.countByExample(oe2);

        OrderExample oe3=new OrderExample();
        OrderExample.Criteria oec3 = oe3.createCriteria();
        //本月预约================================================================
        oec3.andOrderdateGreaterThanOrEqualTo(firstDay4ThisMonth);
        long thisMonthOrderNumber = orderMapper.countByExample(oe3);
        oec3.andOrderstatusEqualTo("已到诊");
        //本月到诊================================================================
        long thisMonthVisitsNumber = orderMapper.countByExample(oe3);

        map.put("reportDate", DateUtils.parseDate2String(DateUtils.getToday()));
        map.put("todayNewMember",todayNewMember);
        map.put("totalMember",totalMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);

        List<Map<String, Object>> hotSetmeal = setMealMapper.findHotSetmeal();
        map.put("hotSetmeal",hotSetmeal);
        return map;
    }
}
